package com.github;

import com.github.io.OutPutFileDataTradition;
import com.github.io.OutputFileData;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import static com.github.MyAutoTestContext.*;

public class Common {

//    public static void main(String[] args) {
//        String[] commadn = {"curl", "http://localhost:8019/admin/login","-X","POST",
//                "-H","Content-Type:application/json","-d","{\"\"\"param\"\"\":\"\"\"GQSJrWy/mwjzSvKdp5n0XyKdW60rWJH47yFD57zPuhnvM1gaCogZpgH0qCmHxsZbyB1MW20chk5R5+lAveABfdearV2/xTeycsLxJek8t91fzBoNqRfXse9BsRqR+8/QE7iCzFfNYyqSMqOdcxixXvchYKodCtEIG8joVxaSEts=\"\"\"}\n"};
//
//        String s = execCurl(commadn);
//        System.out.printf(s);
//    }


    public OutputFileData tradition = new OutPutFileDataTradition();
    public final String interfaceName = currentPath() + PATH + "interface" + File.separator;
    public final String alias = currentPath() + PATH + "alias" + File.separator;
    public final String header = currentPath() + PATH + "header" + File.separator;
    public final String method = currentPath() + PATH + "method" + File.separator;
    public final String param = currentPath() + PATH + "param" + File.separator;
    public final String response = currentPath() + PATH + "response" + File.separator;
    public final String userTemp = currentPath() + PATH + "temp" + File.separator + "user" + File.separator;
    public final String env = currentPath() + PATH + "env" + File.separator;
    public final String user = currentPath() + PATH + "user" + File.separator;
    public final String current = "current";
    public final String userCurrent = "userCurrent";
    public final String notFound = "\tkey is not found";

    public boolean isEmpty(String str) {
        return str == null || str.trim().equals("");
    }

    public final Function<String, File> userPath = (userName -> {
        String path = user + File.separator + "token" + File.separator + userName;
        File file = new File(user + File.separator + "token");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    });

    public Map<String, String> valueToMap(String value) {
        Map<String, String> map = new HashMap<>();
        boolean flag = false;
        String key = BLANK;
        String[] split = value.split(DBLANK);
        for (String str : split) {
            if (flag && str != BLANK) {
                map.put(key, str);
                flag = false;
            }
            if (str.startsWith("-")) {
                key = str;
                flag = true;
            }
        }
        return map;
    }

    public String currentPath() {
        File file = new File(BLANK);
        String returnValue = BLANK;
        try {
            returnValue = file.getCanonicalPath();
            file = new File(returnValue + File.separator + "currentPath");
            if (file.exists()) {
                return read(file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//            System.out.println(file.getCanonicalFile());

        return returnValue;
    }


    public String setCurrentPath(String path) {
        try {
            File file = new File(BLANK);
            file = new File(file.getCanonicalFile().getPath() + File.separator + "currentPath");
            if (!file.exists()) {
                file.createNewFile();
            } else {
                tradition.outPutFile(path.getBytes(), file);
                return read(file);
            }
            String canonicalPath = file.getCanonicalPath();

            return canonicalPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        throw new IllegalArgumentException("current path is not avali");

    }


//    public String currentPath(String path) {
//        if(path!=null && path.trim().length()!=0){
//
//        }
//        currentPath();
//    }

    public String execCurl(String[] cmds) {
        ProcessBuilder process = new ProcessBuilder(cmds);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            System.out.println("\t" + builder.toString());
            return builder.toString();
        } catch (IOException e) {
            System.out.print("error");
            e.printStackTrace();
        }
        return null;
    }

    public static String read(File file) {
        FileInputStream fis = null;
        String str = "";
        try {
            fis = new FileInputStream(file);
            byte[] buf = new byte[1024];
            int length = 0;

            while ((length = fis.read(buf)) != -1) {
                str += new String(buf, 0, length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return str;
    }

}


